package com.moli.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moli.constans.SystemConstants;
import com.moli.domain.ResponseResult;
import com.moli.domain.entity.Article;
import com.moli.domain.entity.Category;
import com.moli.domain.vo.CategoryVo;
import com.moli.domain.vo.PageVo;
import com.moli.mapper.CategoryMapper;
import com.moli.service.ArticleService;
import com.moli.service.CategoryService;
import com.moli.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author: zzb
 * @Date: 2023/06/06/23:12
 * @Description: 分类表(Category)表服务实现类
 */
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

	@Autowired
	private ArticleService articleService;

	@Override
	public ResponseResult getCategoryList() {
		//查询文章表  状态为已发布的文章
		LambdaQueryWrapper<Article> articleWrapper = new LambdaQueryWrapper<>();
		articleWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
		List<Article> articleList = articleService.list(articleWrapper);
		//获取文章的分类id，并且去重
		Set<Long> categoryIds = articleList.stream()
				.map(article -> article.getCategoryId())
				.collect(Collectors.toSet());

		//查询分类表
		List<Category> categories = listByIds(categoryIds);
		categories = categories.stream().
				filter(category -> SystemConstants.STATUS_NORMAL.equals(category.getStatus()))
				.collect(Collectors.toList());
		//封装vo
		List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(categories, CategoryVo.class);

		return ResponseResult.okResult(categoryVos);
	}

	@Override
	public List<CategoryVo> listAllCategory() {
		LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(Category::getStatus, SystemConstants.NORMAL);
		List<Category> list = list(wrapper);
		List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(list, CategoryVo.class);
		return categoryVos;
	}

	@Override
	public PageVo selectCategoryPage(Category category, Integer pageNum, Integer pageSize) {
		LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper();

		queryWrapper.like(StringUtils.hasText(category.getName()), Category::getName, category.getName());
		queryWrapper.eq(Objects.nonNull(category.getStatus()), Category::getStatus, category.getStatus());

		Page<Category> page = new Page<>();
		page.setCurrent(pageNum);
		page.setSize(pageSize);
		page(page, queryWrapper);

		//转换成VO
		List<Category> categories = page.getRecords();

		PageVo pageVo = new PageVo();
		pageVo.setTotal(page.getTotal());
		pageVo.setRows(categories);
		return pageVo;
	}
}





